/*
 * @Author       : Hejh(3010733382@qq.com)
 * @Version      : V1.0
 * @Date         : 2024-01-29 18:14:18
 * @Description  : 角色相关
 */
import { defineStore } from 'pinia'
import { getDepartmentByPage, getUserDepartmentByPage } from '@/api/department'
import type { DepartmentRes } from '@/types/department'

interface DepartmentStore {
  fullDepList: DepartmentRes[] // 部门选择器的数组，默认是一个完整的，包含根部门(id为1的)
}

export default defineStore('department', {
  state(): DepartmentStore {
    return {
      fullDepList: []
    }
  },
  actions: {
    setDullDepList(fullDepList: DepartmentRes[]) {
      this.fullDepList = fullDepList
    },
    async getFullDepList() {
      const res = await getUserDepartmentByPage({
        page: 1,
        pageSize: 9999,
        pid: '-1'
      })

      const data = res.data.items.map((item) => {
        return {
          ...item,
          label: item.name,
          key: item.id,
          isLeaf: !!item.isLeaf,
          isUserLeaf: !!item.isUserLeaf
        }
      })

      // 转换为树形结构
      this.setDullDepList(data)
    }
  }
})
